<?php
/**
 * Created by PhpStorm.
 * User: lingyun
 * Date: 2018/5/4
 * Time: 9:48
 * Desc: 达达订单接口回调
 */
namespace app\index\controller;
use think\Controller;
use think\Db;
use service\DataService;
use app\comm\service\DadaOpenapi;

class Dada extends Controller{
    /**
     * @Author  lingyun
     * @Desc    达达订单配送回调测试
     * 2018-05-04
     *        $config_arr['url'] = "http://newopen.imdada.cn/api/order/accept";                 //模拟接受订单 2
    //        $config_arr['url'] = "http://newopen.imdada.cn/api/order/fetch";                 //模拟完成取货  3
    //        $config_arr['url'] = "http://newopen.imdada.cn/api/order/finish";                //模拟完成订单  4
    //        $config_arr['url'] = "http://newopen.imdada.cn/api/order/cancel";                //模拟取消订单  5
    //        $config_arr['url'] = "http://newopen.imdada.cn/api/order/expire";                //模拟订单过期  7
     */
    public function index(){
//        $shuju = '{"signature":"4722a4147636dc9413ebc13e7207280d","client_id":"273982592398117","order_id":"20180507163724521024840013","order_status":5,"cancel_reason":"订单填写错误","cancel_from":2,"dm_id":0,"update_time":1525682250}';
//        $notify = json_decode($shuju,true);      //回调响应参数
//        file_put_contents('dada.txt',file_get_contents('php://input'));
        $notify = json_decode(file_get_contents('php://input'),true);      //回调响应参数
//        $delivery_order = db('order_delivery')->alias('a')->join('ys_order b','a.order_id=b.id','LEFT')->where('a.order_sn',$notify['order_id'])->field('a.id,b.order_sn,a.order_id')->find();
//        halt($delivery_order);
        $config_arr = config('dada');
        $config_arr['url'] = "http://newopen.imdada.cn/api/order/status/query";          //订单详情查询
        $dada_obj = new DadaOpenapi($config_arr);
        $config_arr['order_id'] = $notify['order_id'];
        $res = $dada_obj->makeRequest($config_arr);
//        $result = $dada_obj->getResult();           //查询订单详情信息
        if(!$res){
            $result = $dada_obj->getResult();           //查询订单详情信息
            $order_delivery = db('order_delivery')->where('order_sn',$result['orderId'])->find();            //配送订单详情
            $order = db('order')->where('id',$order_delivery['order_id'])->find();                  //订单信息
            if($result['statusCode'] == 2){             //接受订单
                $order_action = db('order_action')->where(['order_id'=>$order['id'],'order_status'=>2])->find();
                if(empty($order_action)){               //没有订单状态信息
                    $data_order_action = [                  //订单动作详情信息
                        'uid'=>$order['uid'],
                        'order_id'=>$order['id'],
                        'action_user'=>0,
                        'order_status'=>2,          //已接单
                        'pay_status'=>1,
                        'status_desc'=>'订单已接单',
                        'storeid'=>$order['store_id'],
                        'addtime'=>strtotime($result['acceptTime'])
                    ];
                    db('order')->where('id',$order['id'])->update(['remark'=>'配送员已接单']);
                    db('order_action')->insert($data_order_action);         //插入订单动作信息
                }
            }else if($result['statusCode'] == 3){                       //订单已经取货
                $order_action = db('order_action')->where(['order_id'=>$order['id'],'order_status'=>3])->find();
                if(empty($order_action)){
                    $data_order_action =[
                        'uid'=>$order['uid'],
                        'order_id'=>$order['id'],
                        'action_user'=>0,
                        'order_status'=>3,          //已接单
                        'pay_status'=>1,
                        'status_desc'=>'已取货',
                        'storeid'=>$order['store_id'],
                        'addtime'=>strtotime($result['fetchTime'])
                    ];
                    db('order')->where('id',$order['id'])->update(['order_status'=>3,'remark'=>'配送员已取货']);
                    db('order_action')->insert($data_order_action);         //插入订单动作信息
                }
            }else if($result['statusCode'] == 4){
                $order_action = db('order_action')->where(['order_id'=>$order['id'],'order_status'=>4])->find();
                if(empty($order_action)){
                    $data_order_action =[
                        'uid'=>$order['uid'],
                        'order_id'=>$order['id'],
                        'action_user'=>0,
                        'order_status'=>4,          //已完成
                        'pay_status'=>1,
                        'status_desc'=>'订单已完成',
                        'storeid'=>$order['store_id'],
                        'addtime'=>strtotime($result['finishTime'])
                    ];
                    if($order['order_status'] >= 3){            //订单3状态是发货状态
                        db('order')->where('id',$order['id'])->update(['order_status'=>4,'remark'=>'订单已完成']);
                    }
                    db('order_details')->where(['order_id'=>$order['id'],'status'=>3])->update(['status'=>4]);      //更新订单状态
                    db('order_action')->insert($data_order_action);         //插入订单动作信息
                }
            }else if($result['statusCode'] == 5){               //订单取消
                $order_action = db('order_action')->where(['order_id'=>$order['id'],'order_status'=>5])->find();
                if(empty($order_action)){
                    $data_order_action =[
                        'uid'=>$order['uid'],
                        'order_id'=>$order['id'],
                        'action_user'=>0,
                        'order_status'=>8,          //已完成
                        'pay_status'=>1,
                        'status_desc'=>'订单已关闭',
                        'storeid'=>$order['store_id'],
                        'addtime'=>strtotime($result['cancelTime'])
                    ];
//                if($order['order_status'] >= 3){            //订单3状态是发货状态
                    db('order')->where('id',$order['id'])->update(['order_status'=>8,'remark'=>'订单已关闭']);
//                }
                    db('order_details')->where(['order_id'=>$order['id'],'status'=>3])->update(['status'=>8]);      //更新订单状态
                    db('order_action')->insert($data_order_action);         //插入订单动作信息
                }
            }else if($result['statusCode'] == 7){
                $order_action = db('order_action')->where(['order_id'=>$order['id'],'order_status'=>5])->find();
                if(empty($order_action)){
                    $data_order_action =[
                        'uid'=>$order['uid'],
                        'order_id'=>$order['id'],
                        'action_user'=>0,
                        'order_status'=>8,          //已完成
                        'pay_status'=>1,
                        'status_desc'=>'订单已关闭',
                        'storeid'=>$order['store_id'],
                        'addtime'=>strtotime($result['cancelTime'])
                    ];
//                if($order['order_status'] >= 3){            //订单3状态是发货状态
                    db('order')->where('id',$order['id'])->update(['order_status'=>8,'remark'=>'订单已关闭']);
//                }
                    db('order_details')->where(['order_id'=>$order['id'],'status'=>3])->update(['status'=>8]);      //更新订单状态
                    db('order_action')->insert($data_order_action);         //插入订单动作信息
                }
            }
        }
    }


    public function delivery_order_action(){
        $config_arr = config('dada');
//        $config_arr['url'] = "http://newopen.qa.imdada.cn/api/order/accept";                //模拟接受订单 3
//        $config_arr['url'] = "http://newopen.qa.imdada.cn/api/order/fetch";                 //模拟完成取货
//        $config_arr['url'] = "http://newopen.qa.imdada.cn/api/order/finish";                //模拟完成订单
//        $config_arr['url'] = "http://newopen.qa.imdada.cn/api/order/cancel";                //模拟取消订单
        $config_arr['url'] = "http://newopen.qa.imdada.cn/api/order/expire";                //模拟订单过期
//        $config_arr['url'] = "http://newopen.qa.imdada.cn/api/order/status/query";          //订单详情查询
        $dada_obj = new DadaOpenapi($config_arr);
        $data_arr['order_id'] = '20180504094505494853550013';
        $result = $dada_obj->makeRequest($data_arr);
        if(!$result){
            $config_arr['url'] = "http://newopen.qa.imdada.cn/api/order/status/query";
            $dada_obj = new DadaOpenapi($config_arr);
            $data_arr['order_id'] = '20180504094505494853550013';
            $result = $dada_obj->makeRequest($data_arr);
//            if(!empty($result)){
            echo $dada_obj->getCode();
            echo $dada_obj->getStatus();
            echo $dada_obj->getMsg();
            halt($dada_obj->getResult());
//            }
        }
        halt($result);
    }

    /**
     * @Author  lingyun
     * @Desc    预查询订单运费
     * 2018-05-11
     */
    public function count_delivery_fee(){
        file_put_contents('dada.txt',file_get_contents('php://input'));
        $notify = json_decode(file_get_contents('php://input'),true);      //回调响应参数
        file_put_contents('dada.txt',file_get_contents('php://input'));
    }


}